package com.wang.blog.controller;

import com.alibaba.fastjson.support.spring.annotation.FastJsonFilter;
import com.alibaba.fastjson.support.spring.annotation.FastJsonView;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.wang.blog.constant.ResultCode;
import com.wang.blog.model.MeArticle;
import com.wang.blog.model.MeArticleBody;
import com.wang.blog.model.MeTag;
import com.wang.blog.model.SysUser;
import com.wang.blog.ov.ArticleVo;
import com.wang.blog.ov.PageVo;
import com.wang.blog.result.Result;
import com.wang.blog.service.ArticleService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王益波
 * @version 1.0
 * @date 2020/9/6 8:45
 */
@RestController
@RequestMapping(value = "/articles")
@CrossOrigin
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping
    @FastJsonView(
            exclude = {
                    @FastJsonFilter(clazz = MeArticle.class, props = {"body", "category", "comments"}),
                    @FastJsonFilter(clazz = MeTag.class, props = {"id", "avatar"}) },
            include = {@FastJsonFilter(clazz = SysUser.class, props = {"nickname"}),
                    @FastJsonFilter(clazz=MeTag.class,props={"tagname"})
            })
    public Result listArticles(ArticleVo article, PageVo page){
        List<MeArticle> articles = articleService.listArticles(article, page);
        return Result.success(articles);
    }

    @GetMapping("/delete/{id}")
    public Result deleteArticleById(@PathVariable("id") Integer id){
    Result r=new Result();
    if(null==id){
        r.setResultCode(ResultCode.PARAM_IS_BLANK);
        return r;
    }
    articleService.deleteArticleById(id);
    r.setResultCode(ResultCode.SUCCESS);
    return r;
    }

    @PostMapping("/update")
    public Result updateArticle(@RequestBody MeArticle article){
        Result r=new Result();
        if(null==article.getId()){
            r.setResultCode(ResultCode.SUCCESS);
            return r;
        }
        Integer articledId=articleService.updateArticle(article);
        r.setResultCode(ResultCode.SUCCESS);
        r.simple().put("articledId",articledId);
        return r;
    }

    @GetMapping("/listArchives")
    public Result listArchives(){
        return Result.success(articleService.listArchives());
    }

    @RequestMapping(value = "/hot")
    @FastJsonView(include = {@FastJsonFilter(clazz=MeArticle.class,props = {"id","title"})})
    public Result listHotArticles() {
        int limit = 6;

        List<MeArticle> articles = articleService.listHotArticles(limit);

        return Result.success(articles);
    }

    @GetMapping("/new")
    @FastJsonView(include = {@FastJsonFilter(clazz = MeArticle.class, props = {"id","title"})})
    public Result listNewArticles() {
        int limit = 6;
        List<MeArticle> articles = articleService.listNewArticles(limit);

        return Result.success(articles);
    }

    @GetMapping("/{id}")
    @FastJsonView(
            exclude = {
                    @FastJsonFilter(clazz = MeArticle.class, props = {"comments"}),
                    @FastJsonFilter(clazz = MeArticleBody.class, props = {"contentHtml"})
            })
    public Result getArticleById(@PathVariable("id") Integer id) {

        Result r = new Result();

        if (null == id) {
            r.setResultCode(ResultCode.PARAM_IS_BLANK);
            return r;
        }

        MeArticle article = articleService.getArticleById(id);

        r.setResultCode(ResultCode.SUCCESS);
        r.setData(article);
        return r;
    }

    @GetMapping("/view/{id}")
    @FastJsonView(
            exclude = {
                    @FastJsonFilter(clazz = MeArticle.class, props = {"comments"}),
                    @FastJsonFilter(clazz = MeArticleBody.class, props = {"contentHtml"}),
                    @FastJsonFilter(clazz = MeTag.class, props = {"avatar"})},
            include = {@FastJsonFilter(clazz = SysUser.class, props = {"id", "nickname", "avatar"})})

    public Result getArticleAndAddViews(@PathVariable("id") Integer id) {

        Result r = new Result();

        if (null == id) {
            r.setResultCode(ResultCode.PARAM_IS_BLANK);
            return r;
        }

        MeArticle article = articleService.getArticleAndAddViews(id);

        r.setResultCode(ResultCode.SUCCESS);
        r.setData(article);
        return r;
    }

    @GetMapping("/category/{id}")
    @FastJsonView(
            exclude = {
                    @FastJsonFilter(clazz = MeArticle.class, props = {"body", "category", "comments"}),
                    @FastJsonFilter(clazz = MeTag.class, props = {"id", "avatar"})},
            include = {@FastJsonFilter(clazz = SysUser.class, props = {"nickname"})})

    public Result listArticlesByCategory(@PathVariable Integer id) {
        List<MeArticle> articles = articleService.listArticlesByCategory(id);

        return Result.success(articles);
    }


    @PostMapping("/publish")
    public Result saveArticle(@Validated @RequestBody MeArticle article) {

        Integer articleId = articleService.publishArticle(article);

        Result r = Result.success();
        r.simple().put("articleId", articleId);
        return r;
    }


}
